package com.lfg.leetcode.leetcode100;

import com.lfg.leetcode.leetcode0.TreeNode;

public class No114flatten {

    public static void main(String[] args) {
        TreeNode root = TreeNode.stringToTreeNode("[1,2,5,3,4,null,6]");
        flatten(root);
        System.out.println("[" + root + "]");
    }

    public static void flatten(TreeNode root) {
        TreeNode ret = new TreeNode(1);
        flatten(root, ret);
        if (ret.right == null) {
            return;
        }
        root.val = ret.right.val;
        root.left = null;
        root.right = ret.right.right;
    }

    public static TreeNode flatten(TreeNode root, TreeNode ret) {
        if (root == null) {
            return ret;
        }
        ret.right = new TreeNode(root.val);
        ret = ret.right;
        ret = flatten(root.left, ret);
        ret = flatten(root.right, ret);
        return ret;
    }
}
